const  Round =qguan("round")
const  Block =qguan("block")
const  Row =qguan("row")

/**
 * 程序结构
 */
export default class Sentence extends Round{

    constructor(){
        super()
        this.list.push(new Block())
        this.list[0].list.push(new Row());

        for(let i=0;i<2;i++){
            this.addData(new Row(),new Block())
        }

    }

    /**
     * 添加主动的动作执行
     * @param row
     */
    addRun(row){
       this. blockAddRow(1,row)
    }
    addData(row,data){
        this.list[0].list.push(row);
        this.list.push(data)
        return this.list.length-1;
    }

    saveRow(id,row){
        this.list[0].list[id]=row;
    }
    findRow(id){
        return  this.list[0].list[id]
    }
    blockAddRow(id,row){
        this.list[id].list.push(row);
        return this.list[id].list.length-1;
    }
    blockFindRow(bid,id){
        return  this.list[bid].list[id]
    }
    blockSaveRow(bid,id,row){
           this.list[bid].list[id]=row;
    }





    /**
     * 修改 row 的值
     * @param rowId
     * @param linkId
     * @param value
     */
    saveRowValue(rowId,linkId,value){

        let _this=this;
        let row=this.findRow(rowId)
        row.save(linkId,value)
        this.saveRow(linkId,row)

    }
    //获取运行数据，到新的对象里面
    findRun(id){
        let res=new    Sentence();
        this._findRun(id,res)
        return res;
    }

    _findRun(id,res){
        let dataRow=this.list[id].list[0]
       let  rowType= this.findRow(id);
        let id1= res.addData(new Row(rowType.find(4)),new Block())
        if(rowType.isValue(4,"2")||rowType.isValue(4,"1")){
            let idr1=0;
            let  idr2=0;
            let idr3=0;
            if(dataRow.isZero(1)){
                idr1=  this._findRun(dataRow.find(1),res )
            }
            if(dataRow.isZero(2)){
                idr2=  this._findRun(dataRow.find(2),res )
            }
            if(dataRow.isZero(3)){
                idr3=  this._findRun(dataRow.find(3),res )
            }
            res.blockAddRow(id1,new Row(idr1,idr2,idr3,"2"));
        }else {
            res.blockAddRow(id1,dataRow);
        }
         return id1;


    }
    //获取运行数据导数组里面
    findRunData(id){
        let res=new    Array();
        let con=new Array();
        let data=new Array();
        let find=new Array();
        let dataRow=this.list[id].list[0]
        this._findRunData(dataRow.find(1),con)
        this._findRunData(dataRow.find(2),data)

        this._findRunData(dataRow.find(3),find)
        res.push(con)
        res.push(data)
        res.push(find)
        return res;
    }

    _findRunData(id,res){
        let dataRow=this.list[id].list[0]
        let  rowType= this.findRow(id);
        if(rowType.isValue(4,"2")||rowType.isValue(4,"1")){
            !dataRow.isZero(1) ? this._findRunData(dataRow.find(1),res ):"";
            !dataRow.isZero(2) ? this._findRunData(dataRow.find(2),res ):"";
        }else {
            res.push(dataRow.find(4))
        }


    }



}